﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    /// <summary>
    /// 日志管理类
    /// </summary>
    public sealed class LogHelper
    {
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="logContent"></param>
        public static void WriteLog(string logContent)
        {
            try
            {
                string appName = System.Web.HttpContext.Current.Server.MapPath("~/");
                string strFilePath = appName + "Log\\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
                if (!Directory.Exists(appName + "Log"))
                {
                    Directory.CreateDirectory(appName + "Log");
                }
                if (!File.Exists(strFilePath))
                {
                    FileStream fs = File.Create(strFilePath);
                    fs.Close();
                }
                StreamWriter sw = new StreamWriter(strFilePath, true, System.Text.Encoding.UTF8);
                sw.WriteLine(logContent + ",");
                sw.Close();
            }
            catch (Exception ex)
            {
            }
        }

        public static void WriteErrorLog(string logContent)
        {
            try
            {
                string appName = System.Web.HttpContext.Current.Server.MapPath("~/");
                string strFilePath = appName + "Log\\" + "Error\\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
                if (!Directory.Exists(appName + "Log\\" + "Error\\"))
                {
                    Directory.CreateDirectory(appName + "Log\\" + "Error\\");
                }
                if (!File.Exists(strFilePath))
                {
                    FileStream fs = File.Create(strFilePath);
                    fs.Close();
                }
                StreamWriter sw = new StreamWriter(strFilePath, true, System.Text.Encoding.UTF8);
                sw.WriteLine(DateTime.Now.ToString() + "\r\n" + logContent);
                sw.Close();
            }
            catch (Exception ex)
            {
            }
        }
    }
}